package com.yunshipei.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yunshipei.common.ExitApps;
import com.yunshipei.common.Globals;
import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.BuildConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.model.MainExtraBean;
import com.yunshipei.model.UserInfo;
import com.yunshipei.model.YspEvent;
import com.yunshipei.ui.activity.DeveloperActivity;
import com.yunshipei.ui.activity.MultiActivity;
import com.yunshipei.utils.BaseUtil;

import butterknife.Bind;
import butterknife.OnClick;
import io.rong.eventbus.EventBus;

public class MeFragment extends BaseBodyFragment {

    public static final int REQUEST_CODE = 1001;

    public static MeFragment newInstance(MainExtraBean mainExtraBean) {
        MeFragment meBodyFragment = new MeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARGS_CUSTOMER_MAIN_DATA, mainExtraBean);
        meBodyFragment.setArguments(bundle);
        return meBodyFragment;
    }

    @Bind(R.id.tv_company_name)
    protected TextView tvCompanyName;
    @Bind(R.id.iv_right_icon)
    protected ImageView ivRightIcon;
    @Bind(R.id.title_bar_content)
    protected RelativeLayout rlTitleContent;

    @Override
    public int viewLayout() {
        return R.layout.fragment_me_body;
    }

    @Bind(R.id.tv_version_name)
    protected TextView tvAppVersion;
    @Bind(R.id.fl_developer)
    protected FrameLayout mDeveloperFl;
    @Bind(R.id.line_developer)
    protected View mLineDeveloper;

    private UserInfo userInfo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mMainExtraBean != null) {
            this.userInfo = mMainExtraBean.getUserInfo();
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        ivRightIcon.setVisibility(View.INVISIBLE);
      //  tvCompanyName.setText(mMainExtraBean.getCompanyTitle());
        tvCompanyName.setText("中可OA");
        rlTitleContent.setBackgroundColor(EnterConfig.getInstance().getGlobalColor());
        String version = BaseUtil.getVersionName();
        tvAppVersion.setText(BuildConfig.DEBUG ? "内测版" + version : version);
        CustomerInfoFragment customerInfoFragment = CustomerInfoFragment.newInstances(userInfo);
        getChildFragmentManager().beginTransaction().replace(R.id.fl_customer_detail, customerInfoFragment, CustomerInfoFragment.class.getName()).commit();
        boolean showDeveloperMode = YspPreferences.getInstance().getSharedPreferences().getBoolean(Globals.YSP_DEBUG, false);
        if (showDeveloperMode) {
            mDeveloperFl.setVisibility(View.VISIBLE);
            mLineDeveloper.setVisibility(View.VISIBLE);
        } else {
            mDeveloperFl.setVisibility(View.GONE);
            mLineDeveloper.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @OnClick({R.id.fl_settings, R.id.logout, R.id.rl_about_enterplorer, R.id.fl_developer})
    public void onSubmit(View v) {
        switch (v.getId()) {
            case R.id.fl_settings:
                String settingTargetName = SettingsFragment.class.getName();
                if (isPad()) {
                    EventBus.getDefault().post(new YspEvent.PadClick(settingTargetName));
                } else {
                    startActivity(new MultiActivity.MultiActivityIntentBuilder(mContext).setMainData(mMainExtraBean).setTargetPartClazzName(settingTargetName).getIntent());
                }
                break;
            case R.id.rl_about_enterplorer:
//                String aboutTargetName = AboutFragment.class.getName();
//                if (isPad()) {
//                    EventBus.getDefault().post(new YspEvent.PadClick(aboutTargetName));
//                } else {
//                    startActivityForResult(new MultiActivity.MultiActivityIntentBuilder(mContext).setMainData(mMainExtraBean).setTargetPartClazzName(aboutTargetName).getIntent(), REQUEST_CODE);
//                }
                break;
            case R.id.logout:
                new ExitApps(getActivity()).showConfirm();
                break;
            case R.id.fl_developer:
                startActivity(new DeveloperActivity.DeveloperIntentBuilder(getActivity()).setUserInfo(userInfo).getIntent());
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null && resultCode == AboutFragment.RESULT_CODE && requestCode == REQUEST_CODE) {
            boolean openDeveloperModel = data.getBooleanExtra(AboutFragment.EXTRA_RESULT_DATA, false);
            developModeChanged(openDeveloperModel);
        }
    }

    protected void developModeChanged(boolean show) {
        if (show) {
            mDeveloperFl.setVisibility(View.VISIBLE);
            mLineDeveloper.setVisibility(View.VISIBLE);
        } else {
            mDeveloperFl.setVisibility(View.GONE);
            mLineDeveloper.setVisibility(View.GONE);
        }
    }

    public void onEventMainThread(YspEvent.DevelopModeChange developModeChange) {
        if (isPad()) {
            developModeChanged(developModeChange.open);
        }
    }
}
